home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1682 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  100 lines

  1. Path: ix.netcom.com!ix-hou7-11
  2. From: Jackson7@ix.netcom.com (T. Hubbard)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer-to-Double as Function Arg
  5. Date: 16 Jan 1996 07:15:45 GMT
  6. Organization: Netcom
  7. Message-ID: <4dfjb1$pln@ixnews5.ix.netcom.com>
  8. References: <4dfccl$j5h@colossus.holonet.net>
  9. NNTP-Posting-Host: ix-hou7-11.ix.netcom.com
  10. X-NETCOM-Date: Mon Jan 15 11:15:45 PM PST 1996
  11. Summary: pointer to pointer example.
  12. Keywords: pointers, **
  13. X-Newsreader: News Xpress Version 1.0 Beta #4
  14.  
  15. In article <4dfccl$j5h@colossus.holonet.net>,
  16.    mitch@news.mdli.com (Mitch Miller) wrote:
  17. >I'm having a problem with a function that takes a
  18. >couple of pointer-to-double arguments, initializes then
  19. >and assigns values to them in an array style:
  20. >
  21. >Function is something like:
  22. >
  23. >    int GetValue( long iDim, double * Values1, double *
  24. >      Values2 )
  25. >    {
  26. >        int iter;
  27. >        Values1 = (double *) malloc( iDim * sizeof( double ));
  28. >        if (Values1 == NULL )
  29. >            ....
  30. >        Values2 = (double *) malloc( iDim * sizeof( double ));
  31. >        if (Values2 == NULL )
  32. >            ...
  33. >
  34. >        for (iter=1; iter<iDim; iter++)
  35. >        {
  36. >            Values1[iter] = ...;
  37. >            Values2[iter] = ...;
  38. >
  39. >        }
  40. >
  41. >        return 1;
  42. >    }
  43. >
  44. >Call from main:
  45. >
  46. >    double * Ptr1;
  47. >    double * Ptr2;
  48. >
  49. >    if (!GetValue( iSize, Ptr1, Ptr2 ) )....
  50. >
  51. >
  52. >When I look at the values of Values1 and Values2 in GetValue, they have
  53. >the correct values.
  54. >
  55. >However, back in main, they have NULL values.
  56. >
  57. >If I make Ptr1 and Ptr2 global variables so that they are not included
  58. >in the function calls, the code works fine.
  59. >
  60. >I've checked the FAQs, but couldn't find anything quite like this.
  61. >
  62. >Thanks in advance... 
  63.  
  64. Hope this helps.....
  65.  
  66. You may want to try using a pointer to a pointer.  This will avoid that nasty 
  67. pass by reference problem you have been experiencing.
  68.  
  69.     int GetValue( long iDim, double **Values1, double **Values2 )
  70.     {
  71.         int iter;
  72.         *Values1 = (double *) malloc( iDim * sizeof( double ));
  73.         if (*Values1 == NULL )
  74.             ....
  75.         *Values2 = (double *) malloc( iDim * sizeof( double ));
  76.         if (*Values2 == NULL )
  77.             ...
  78. /* Watch out by starting at subcript 1 rather than 0. */
  79.         for (iter=1; iter<iDim; iter++)
  80.         {
  81.             (*Values1)[iter] = ...;
  82.             (*Values2)[iter] = ...;
  83.  
  84.         }
  85.  
  86.         return 1;
  87.     }
  88.  
  89. Call from main:
  90.  
  91.     double * Ptr1;
  92.     double * Ptr2;
  93.  
  94. /* Why use the if you only return 1, it will always evaluate to true. */
  95.  
  96.     if (!GetValue( iSize, &Ptr1, &Ptr2 ) )....
  97.     
  98. :)
  99. Jackson7@ix.netcom.com
  100.